﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Abp.Domain.Entities.Auditing;

namespace DHJ.Cost.TreeStructure
{
    public class TreeNodeBase : FullAuditedEntity<long>
    {
        protected TreeNodeBase()
        {
        }

        public TreeNodeBase(string nodeName, string nodeValue)
        {
            NodeCode = GenerateId();
            NodeName = nodeName;
            NodeValue = nodeValue;
        }

        [Required] public string NodeName { get; set; }

        [Required] public string NodeValue { get; set; }

        public string NodeCode { get; set; }


        /// <summary>
        ///     创建长度为16位的唯一编码
        /// </summary>
        /// <returns></returns>
        private static string GenerateId()
        {
            var i = Guid.NewGuid().ToByteArray().Aggregate<byte, long>(1, (current, b) => current * (b + 1));
            return $"{i - DateTime.Now.Ticks:x}";
        }

        public override bool IsTransient()
        {
            throw new NotImplementedException();
        }
    }
}